package com.tg.app.activity.device;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.tange.base.toolkit.StringUtils;
import com.tg.app.R;
import com.tg.app.activity.base.WebBaseActivity;
import com.tg.app.helper.WebViewHelper;
import com.tg.app.view.RoundWebview;
import com.tange.base.toolkit.DimenUtil;



public class AddDeviceHelperActivity extends Activity {
    private String mWebUrl;
    public static final String KEY_WEB_HEIGHT = "key_web_height";
    public static final String KEY_WEB_MARGIN = "key_web_margin";
    public static final String KEY_SHOW_CLOSE = "key_show_close_btn";
    public static final String KEY_FROM_DEVICE_TIP = "key_from_device_tip";
    private int height = 0;
    private boolean showCloseBtn = true;
    private int margin = 0;
    private String from;
    private final WebViewHelper webViewHelper = new WebViewHelper();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        from = getIntent().getStringExtra(KEY_FROM_DEVICE_TIP);
        if (StringUtils.equalsIgnoreCase(from, KEY_FROM_DEVICE_TIP)){
            setContentView(R.layout.activity_device_tip_helper);
        }else{
            setContentView(R.layout.activity_device_add_helper);
        }

        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);//需要添加的语句
        mWebUrl = getIntent().getStringExtra(WebBaseActivity.KEY_WEBURL);
        height = getIntent().getIntExtra(KEY_WEB_HEIGHT, 0);
        margin = getIntent().getIntExtra(KEY_WEB_MARGIN, 0);
        showCloseBtn = getIntent().getBooleanExtra(KEY_SHOW_CLOSE, true);
        initView();

        if (!StringUtils.equalsIgnoreCase(from, KEY_FROM_DEVICE_TIP)){
            findViewById(R.id.device_business_layout).setOnClickListener(v -> finish());
        }

        findViewById(R.id.iv_view_cancel).setOnClickListener(v -> finish());
        findViewById(R.id.iv_view_cancel).setVisibility(showCloseBtn? View.VISIBLE : View.GONE);
    }

    public void setCloseButton(boolean show){
        findViewById(R.id.iv_view_cancel).setVisibility(show? View.VISIBLE : View.GONE);
    }

    public void initView(){
        RoundWebview webView = findViewById(R.id.webview);

        webViewHelper.setContext(this);
        webViewHelper.setWebView(webView);
        webViewHelper.setWebUrl(mWebUrl);
        webViewHelper.initWeb();

        if (StringUtils.equalsIgnoreCase(from, KEY_FROM_DEVICE_TIP)){
            webView.post(new Runnable() {
                @Override
                public void run() {
                    if (height != 0){
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) webView.getLayoutParams();
                        layoutParams.height = height;
                        webView.setLayoutParams(layoutParams);
                    }
                }
            });
            if (margin != 0){
                RelativeLayout linearLayout = findViewById(R.id.ll_layout);
                linearLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) linearLayout.getLayoutParams();
                        layoutParams.topMargin = margin;
                        layoutParams.leftMargin = margin;
                        layoutParams.bottomMargin = margin;
                        layoutParams.rightMargin = margin;
                        linearLayout.setLayoutParams(layoutParams);
                    }
                });
                webView.setRadius((int) DimenUtil.px2dp(AddDeviceHelperActivity.this, margin));
            }
        } else{
            webView.setRadius(0);
        }
    }



}